package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.Index_Bean;
import com.dingwei.superiordiary.bean.ProductsBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class SelectedPresenter extends basePresenter {

    private Contract.SelectedView selectedView;

    public SelectedPresenter(Contract.SelectedView view) {
        super(view);
        this.selectedView = view;
    }

    public void initData() {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.INDEX);
        x.http().post(rp, new myCallback(BaseActivity.context, Index_Bean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                Index_Bean bean = (Index_Bean) baseBean;
                selectedView.initData(bean);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                selectedView.OnFails();
            }
        });
    }

    public void getProducts(final boolean isRefresh, boolean isShowDialog, int page) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.INDEX_PRODUCTS);
        rp.addBodyParameter("page", page+"");
        x.http().post(rp, new myCallback(BaseActivity.context, ProductsBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                ProductsBean bean = (ProductsBean) baseBean;
                if (isRefresh){
                    selectedView.OnRefresh(bean);
                }else {
                    selectedView.OnLoadMore(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                selectedView.OnFails();
            }
        });
    }
}
